"""
    Author:
    Date: 2019-01-22

    服务器归档管理类
"""
import datetime

from entities import call_mgr
from entities import gift_mgr
from entities import pay_mgr
from entities import user_mgr
from entities.consts import *
from utils import db
from utils.log import Log


def archive_server_data():
    """ 归档服务器数据 """
    today = datetime.date.today()
    create_date = '%4d-%02d-%02d' % (today.year, today.month, today.day)
    Log.info(f'Archive server data {create_date}.')

    user_stat = user_mgr.get_user_stat_data()
    total_ticket = user_stat['total_ticket'] if user_stat else 0,
    total_diamond = user_stat['total_diamond'] if user_stat else 0,
    total_exp = user_stat['total_exp'] if user_stat else 0,
    total_user_count = user_stat['total_user_count'] if user_stat else 0,

    total_broadcaster_count = user_mgr.get_certified_broadcaster_count()
    total_pay_amount = pay_mgr.get_total_recharge_amount()
    total_withdraw_amount = 0
    total_follow_count = user_mgr.get_total_follow_count()

    total_agent_count = 0
    total_agent_recruit_count = 0
    total_partner_count = 0
    total_partner_recruit_count = 0

    total_call_seconds = call_mgr.get_total_total_call_seconds()
    total_call_count = call_mgr.get_total_total_call_count()

    total_gift_income = gift_mgr.get_total_income_by_type(TICKET_INPUT_RECEIVE_GIFT)
    total_video_income = gift_mgr.get_total_income_by_type(TICKET_INPUT_SELL_VIDEO)
    total_vchat_income = gift_mgr.get_total_income_by_type(TICKET_INPUT_VIDEO_CHAT)
    total_message_income = gift_mgr.get_total_income_by_type(TICKET_INPUT_RECEIVE_MESSAGE)

    data = {
        'create_date': create_date,

        'total_ticket': total_ticket,
        'total_diamond': total_diamond,
        'total_exp': total_exp,
        'total_user_count': total_user_count,
        'total_broadcaster_count': total_broadcaster_count,

        'total_pay_amount': total_pay_amount,
        'total_withdraw_amount': total_withdraw_amount,

        'total_follow_count': total_follow_count,

        'total_agent_count': total_agent_count,
        'total_agent_recruit_count': total_agent_recruit_count,
        'total_partner_count': total_partner_count,
        'total_partner_recruit_count': total_partner_recruit_count,

        'total_call_seconds': total_call_seconds,
        'total_call_count': total_call_count,

        'total_gift_income': total_gift_income,
        'total_video_income': total_video_income,
        'total_vchat_income': total_vchat_income,
        'total_message_income': total_message_income,
    }
    return insert_consume_record(data)


def insert_consume_record(data):
    """ 插入消费记录 """
    sql = '''
        INSERT INTO cmf_server_daily_archive
        SET         create_date=%(create_date)s,

                    total_ticket=%(total_ticket)s,
                    total_diamond=%(total_diamond)s,
                    total_exp=%(total_exp)s,
                    total_user_count=%(total_user_count)s,
                    total_broadcaster_count=%(total_broadcaster_count)s,

                    total_pay_amount=%(total_pay_amount)s,
                    total_withdraw_amount=%(total_withdraw_amount)s,
                    total_follow_count=%(total_follow_count)s,

                    total_agent_count=%(total_agent_count)s,
                    total_agent_recruit_count=%(total_agent_recruit_count)s,
                    total_partner_count=%(total_partner_count)s,
                    total_partner_recruit_count=%(total_partner_recruit_count)s,

                    total_call_seconds=%(total_call_seconds)s,
                    total_call_count=%(total_call_count)s,

                    total_gift_income=%(total_gift_income)s,
                    total_video_income=%(total_video_income)s,
                    total_vchat_income=%(total_vchat_income)s,
                    total_message_income=%(total_message_income)s;
    '''
    params = data
    affected_rows = db.execute_sql(sql, params)
    return affected_rows == 1


if __name__ == '__main__':
    print('ok')
